package com.yunshipei.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yunshipei.ui.activity.AlarmSetActivity;
import com.yunshipei.enterplorer.R;
import com.yunshipei.ui.view.wheelview.OnWheelScrollListener;
import com.yunshipei.ui.view.wheelview.WheelView;
import com.yunshipei.ui.view.wheelview.adapter.ArrayWheelAdapter;

import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 */
public class ForwardFragment extends BaseFragment {
    private LinearLayout content;
    private WheelView time;
    private WheelView timeType;
    String [] timeArray  = {"1", "5", "10"};
    String[] timeTypeArray = {"分钟", "小时", "天"};

    public ForwardFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_time, container, false);
        content  = (LinearLayout) view.findViewById(R.id.content);
        content.addView(getDataPick());
        return view;
    }

    private View getDataPick() {
        Calendar c = Calendar.getInstance();
        int norYear = c.get(Calendar.YEAR) + 5;
        //获取时间滚轮
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.forward_time_item, null);
        time = (WheelView) view.findViewById(R.id.time);
//        time.setShadowColor(0x00000032, 0x000000C8, 0x00000032);
        ArrayWheelAdapter arrayWheelAdapter1 = new ArrayWheelAdapter<String>(getActivity(), timeArray);
        time.setViewAdapter(arrayWheelAdapter1);
//        arrayWheelAdapter1.setTextColor(0xFF000000);
//        arrayWheelAdapter1.setTextSize(25);
        time.setCyclic(true);
        time.addScrollingListener(scrollListener);
        timeType = (WheelView) view.findViewById(R.id.timeType);
//        timeType.setShadowColor(0x00000032, 0x000000C8, 0x00000032);
        ArrayWheelAdapter arrayWheelAdapter2 = new ArrayWheelAdapter(getActivity(), timeTypeArray);
//        arrayWheelAdapter2.setTextColor(0xFF000000);
//        arrayWheelAdapter2.setTextSize(25);
        timeType.setViewAdapter(arrayWheelAdapter2);
        timeType.setCyclic(true);
        timeType.addScrollingListener(scrollListener);
        time.setVisibleItems(5);
        timeType.setVisibleItems(5);

        time.setCurrentItem(1);
        timeType.setCurrentItem(1);
        return view;
    }

    OnWheelScrollListener scrollListener;

    {
        scrollListener = new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                Log.e("结果时间", "大小:" + (time.getCurrentItem()) + "   单位:" + timeType.getCurrentItem());
                handlerSendMessage();
            }
        };
    }

    @Override
    void handlerSendMessage() {
        Message message = new Message();
        message.what = AlarmSetActivity.FORWARDTIME;
        Bundle bundle = new Bundle();
        bundle.putString("time", timeArray[time.getCurrentItem()]);
        bundle.putString("timeType", timeTypeArray[timeType.getCurrentItem()]);
        message.setData(bundle);
        handler.sendMessage(message);
    }

}
